/*
 * @Author: liuxinyea
 * @Date: 2024-08-18 23:18:32
 * @LastEditors: liuxinye
 * @LastEditTime: 2024-08-20 08:14:54
 * @Description: 僵尸类
 */
import { _decorator, animation, BoxCollider2D, CCInteger, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, Vec3 } from 'cc';
import { ZombieAnimationLayer, ZombieAnimationState, ZombieState } from './Glabol';
import { PeaBullet } from './PeaBullet';
import { Plant } from './Plant';
import { Cell } from './Cell';
const { ccclass, property } = _decorator;

@ccclass('Zombie')
export class Zombie extends Component {
    @property(CCInteger)
    public speed: number = 10;
    @property(CCInteger)
    public healthNum: number = 0; // 生命值
    @property(CCInteger) // 攻击力
    public damage: number = 0;
    // 僵尸的状态，默认是行走状态
    private state: ZombieState = ZombieState.Walking;
    private mCollider: BoxCollider2D = null;
    private mAnimation: animation.AnimationController
    private isAttacked: boolean = false
    private attackTimer: number = 0
    private attackDuration: number = 2;
    private currentPlant: Plant = null
    // 跟场景相关的初始化
    protected onLoad(): void {
        this.mCollider = this.getComponent(BoxCollider2D);
        console.log('Zombie--mCollider', this.mCollider);
        this.attackTimer = this.attackDuration
        if (this.mCollider) {
            this.mCollider.on(Contact2DType.BEGIN_CONTACT, this.onTriggerEnter, this);
        }
        this.mAnimation = this.getComponent(animation.AnimationController);
    }
    onTriggerEnter(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log('Zombie--onTriggerEnter', otherCollider.tag);
        // 根据tag判断类型，tag==1是子弹类型
        if (otherCollider.tag == 1) {
            this.beAttacked(otherCollider)
            return
        }
        // 遇到植物
        if (otherCollider.tag == 2) {
            this.doAttack(otherCollider)   
        }
    }
    // 僵尸被子弹攻击
    beAttacked(otherCollider: Collider2D) {
        // 播放受击动画
        this.mAnimation.setValue('isAttacked', true);
        const bullet: PeaBullet = otherCollider.node.getComponent(PeaBullet)
        if (bullet) {
            bullet.node.destroy()
            this.healthNum -= bullet.damage
            if (this.healthNum <= 0) {
                this.state = ZombieState.Dead
                // 执行死亡动画，执行完成后销毁onDied
                this.mAnimation.setValue('state', ZombieAnimationState.Die)
                return;
            }
            if (this.healthNum <= 10) {
                // 设置动画状态为断头行走或者断头攻击
                if (this.state == ZombieState.Walking) {
                    //设置动画状态为断头行走
                    this.mAnimation.setValue('state', ZombieAnimationState.LoseHeadMove)
                }
                if (this.state == ZombieState.Attack) {
                    //设置动画状态为断头攻击
                    this.mAnimation.setValue('state', ZombieAnimationState.LoseHeadEat)
                }
                // 播放端头动画，触发器触发，播放一次
                this.mAnimation.setValue('isLoseHead', true);
                // 断头之后不再前进
                this.state = ZombieState.Dead;
            }

        }
    }
    // 僵尸攻击植物
    doAttack(otherCollider: Collider2D) {
        // 设置僵尸动画状态为攻击状态，并且播放攻击动画
        // 这里需要判断当前是不是已经断头
        console.log('turn doAttack');
        
        const cell: Cell = otherCollider.node.getComponent(Cell)
        if (cell && cell.currentPlant) {
            // 判断植物类型，做出相关处理，这里需要处理很多种植物和僵尸之间的交互，可以独立封装到一个管理类中
            const plant: Plant = cell.currentPlant.getComponent(Plant)
            if (plant) {
                const animationState: number = this.mAnimation.getValue("state").valueOf() as number
                this.state = ZombieState.Attack
                if (animationState == ZombieAnimationState.LoseHeadMove) {// 端头攻击
                    this.mAnimation.setValue('state', ZombieAnimationState.LoseHeadEat)
                } else {
                    this.mAnimation.setValue('state', ZombieAnimationState.Eating)
                }
                plant.healthNum -= this.damage;
                this.currentPlant = plant
                if (plant.healthNum <= 0) {
                    // 销毁植物，继续前进
                    plant.node.destroy()
                    this.state = ZombieState.Walking
                }
            }
        }
    }
    // 死亡动画播放完成的回调
    onDied() {
        console.log('Zombie--onDied');
        
        this.node.destroy()
    }
    update(deltaTime: number) {
        const position = this.node.position;
        if (this.state === ZombieState.Walking) {
            this.node.setPosition(new Vec3(position.x - deltaTime * this.speed, position.y, 0))
        }
        if(this.state === ZombieState.Attack){
            this.attackTimer -= deltaTime;
            if(this.attackTimer<=0){
                if(this.currentPlant){
                    this.currentPlant.healthNum -= this.damage;
                    if (this.currentPlant.healthNum <= 0) {
                        // 销毁植物，继续前进
                        this.currentPlant.node.destroy()
                        this.mAnimation.setValue('state', ZombieAnimationState.Walking)
                        this.state = ZombieState.Walking
                    }
                }
                this.attackTimer = this.attackDuration
            }
        }
    }
}

